package com.ruoyi.web.controller.system;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.ITaskService;
import com.ruoyi.web.controller.unit.LsyDownLoad;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 任务报送Controller
 * 
 * @author ruoyi
 * @date 2024-06-28
 */
@RestController
@RequestMapping("/system/task")
public class TaskController extends BaseController
{
    @Autowired
    private ITaskService taskService;
    @Resource
    private LsyDownLoad load;
    /**
     * 查询任务报送列表
     */
    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(Task task)
    {
        startPage();
        List<Task> list = taskService.selectTaskList(task);
        return getDataTable(list);
    }

    /**
     * 导出任务报送列表
     */
    @PreAuthorize("@ss.hasPermi('system:task:export')")
    @Log(title = "任务报送", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Task task)
    {
        List<Task> list = taskService.selectTaskList(task);
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        util.exportExcel(response, list, "任务报送数据");
    }

    /**
     * 获取任务报送详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(taskService.selectTaskById(id));
    }

    /**
     * 新增任务报送
     */
    @PreAuthorize("@ss.hasPermi('system:task:add')")
    @Log(title = "任务报送", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Task task)
    {
        return toAjax(taskService.insertTask(task));
    }

    /**
     * 修改任务报送
     */
    @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "任务报送", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Task task)
    {
        return toAjax(taskService.updateTask(task));
    }

    /**
     * 删除任务报送
     */
    @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "任务报送", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(taskService.deleteTaskByIds(ids));
    }

    /**
     * 获取任务报送详细信息
     */


    @PostMapping(value = "/down/{id}")
    public void generateAndDownload(@PathVariable Long id,HttpServletResponse response) throws Exception {
        // 加载模板
       Task task = taskService.selectTaskById(id);
        DownLoadVo vo  = load.getDownLoadVo(task);
        HashMap<String, Object> data=vo.getData();

        String url= vo.getUrl();
        Configure configure =vo.getConfigure();
        // 渲染模板
        XWPFTemplate template = XWPFTemplate.compile(url,configure).render(data);
        // 设置HTTP响应头，使浏览器知道这是一个Word文档
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setHeader("Content-Disposition", "attachment; filename=generatedDocument.docx");
        // 输出到HTTP响应流
        template.write(response.getOutputStream());
        template.close();
    }

}
